﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the API Concepts Framework
 * http://apiconceptsframework.codeplex.com
 *
 * Copyright (C) 2014 eva Kühn, Thomas Scheller, Vienna University of Technology
 * http://www.complang.tuwien.ac.at/eva
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Reflection;
using ApiConceptsFramework.Contract;

namespace ApiConceptsFramework.Concepts.Properties
{
	public class NumberOfParameters : IMeasurableProperty
	{
		internal const int NumberOfParamsForSuggestingReduction = 5;
		//internal const int BaseParameterComplexity = 2;
		internal const float ParameterComplexity = 2.5f;
		//internal const int IncreasedParamComplexityStartIndex = 1;
		internal const int ReturnParameterComplexity = 7; //return parameter is rated separately
		
		internal int NumberOfParamAdaptation = 0; //this value can be changed to adapt the calculation of the number of params

		public float CalculateComplexity(MeasurableTarget target, IDE ide = IDE.VisualStudioOrSimilar)
		{
			if (!(target.Member is MethodInfo) && !(target.Member is ConstructorInfo))
				throw new ArgumentException("target.Member must be a MethodInfo or ConstructorInfo!", "target");
			
			var numberOfParameters = CalculateNumberOfParameters(target);

			var complexity = (numberOfParameters + NumberOfParamAdaptation)
				.CalculateIncrementalComplexity(0, 0, ParameterComplexity, 0);
			return complexity;
		}

		private int CalculateNumberOfParameters(MeasurableTarget target)
		{
			var method = (MethodBase)target.Member;
			bool isExtension = method.IsExtensionMethod();
			int genericParametersCount = method.IsGenericMethod ? method.GetGenericArguments().Length : 0;
			int parametersCount = method.GetParameters().Length;

			var numberOfParameters = CalculateNumberOfParameters(parametersCount, genericParametersCount, isExtension);
			CalculationInfo = numberOfParameters + " parameters";
			return numberOfParameters;
		}

		private static int CalculateNumberOfParameters(int parametersCount, int genericParametersCount, bool isExtension)
		{
			var numberOfParameters = parametersCount + genericParametersCount;
			if (isExtension)
			{
				numberOfParameters--; //when method is an extension: eliminate the first parameter because it is not displayed
				if (genericParametersCount > 0)
					numberOfParameters--; //also eliminate a generic parameter because it is likely to be defined by the extension parameter
			}
			return numberOfParameters;
		}

		public IEnumerable<string> GetSuggestions(MeasurableTarget target, IDE ide = IDE.VisualStudioOrSimilar)
		{
			var numberOfNonGenericInputParams = ((MethodBase)target.Member).GetParameters().Length;

			if (numberOfNonGenericInputParams >= NumberOfParamsForSuggestingReduction)
				yield return
					"Member has a high number of " + numberOfNonGenericInputParams +
					" parameters. Consider reducing parameters by e.g. providing an overload or merging multiple parameters into one.";
		}

		public string CalculationInfo { get; private set; }
	}
}